package com.itheima.service.Impl;

import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> list) {
        if(list!=null&&list.size()>0){
            for (OrderSetting orderSetting : list) {
                Date orderDate = orderSetting.getOrderDate();
                long countByOrderDate = orderSettingDao.findCountByOrderDate(orderDate);
                if(countByOrderDate>0){
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else{
                    orderSettingDao.add(orderSetting);
                }
            }
        }
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                //检查日期是不是存在的
                long countByOrderDate = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (countByOrderDate > 0) {
                    //表示已经存在,那我们就执行更新的操作
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                } else {
                    //表示没有存在过,那我们就执行新增操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        //这个是查询条件
        String begin = date + "-1";
        String end = date + "-31";
        //这个是把查询条件封装在map里面带到dao里面
        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);
        //这个就是调用dao,但是从数据库里面查询到的数据是ordersetting,所以返回的是这个 List<OrderSetting>list
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        //但是返回的这个是和我们前台页面的不合的,因为这个里面封装的数据是
        // private Integer id ;
        // private Date orderDate;//预约设置日期
        // private int number;//可预约人数
        // private int reservations ;//已预约人数
        //而我们前台是下面这个,所以不能直接返回这个,不然前台解析不了的
        // this.leftobj = [
        //                        { date: 1, number: 120, reservations: 1 },
        //                        { date: 3, number: 120, reservations: 1 },
        //                        { date: 4, number: 120, reservations: 120 },
        //                        { date: 6, number: 120, reservations: 1 },
        //                        { date: 8, number: 120, reservations: 1 }
        //                    ];
        List<Map> list1 = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                //这个就是为了把我们查询到的数据给转化为和前台一样的数据名字,这样前台可以解析到
                Map map1 = new HashMap();
                System.out.println(orderSetting.getOrderDate());
                //之所以orderSetting.getOrderDate()之后还getDate是因为这样我们就可以获得的是后面的几号
                int date1 = orderSetting.getOrderDate().getDate();
                System.out.println(date1);
                int number = orderSetting.getNumber();
                int reservations = orderSetting.getReservations();
                map1.put("date", date1);
                map1.put("number", number);
                map1.put("reservations", reservations);
                list1.add(map1);
            }
        }
        return list1;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //判断这个到底有没有经过预约设置
        long countByOrderDate = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if(countByOrderDate>0){
            //这个表示有预约设置了,那就直接修改
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            //这个表示没有进行预约设置,那就直接添加设置
            orderSettingDao.add(orderSetting);
        }
    }
}
